using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;

namespace LetsDoSomeVariations
{
    /// <summary>
    /// FeatureReceiver class to trap the activation, deactivation, installation, or uninstallation of the LetsDoSomeVariations Feature.
    /// </summary>
    public class FeatureReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// Occurs after a Feature is activated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            if (properties == null)
            {
                return;
            }

            // determine the scope of the feature Site or Web
            SPWeb web = null;
            Object parent = properties.Feature.Parent;
            if (parent is SPWeb)
            {
                web = (SPWeb)parent;
            }
            else if (parent is SPSite)
            {
                web = ((SPSite)parent).RootWeb;
            }

            SPFeaturePropertyCollection featureProperties = properties.Feature.Properties;

            bool createVariations = false;
            SPFeatureProperty p1 = featureProperties[Constants.FeatureProperties.CreateVariations];
            if (p1 != null)
            {
                bool.TryParse(p1.Value, out createVariations);
            }

            if (!createVariations)
            {
                return;
            }

            using (SPSite site1 = new SPSite(web.Site.ID))
            {
                using (SPWeb web1 = site1.OpenWeb(web.ID))
                {
                    VariationsComponent.CreateVariations(web1, featureProperties);
                }
            }
        }

        /// <summary>
        /// Occurs when the Feature is deactivated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
        }

        /// <summary>
        /// Occurs after the Feature is installed.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        /// <summary>
        /// Occurs when the Feature is uninstalled.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}